package sz.szsmk.citizencard.ui.dzsmkavtivity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.widget.Toolbar;

import com.chenenyu.router.annotation.Route;
import com.gyf.immersionbar.ImmersionBar;

import org.greenrobot.eventbus.EventBus;

import javax.inject.Inject;

import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.model.network.bean.InBackgroundBean;
import sz.szsmk.citizencard.model.network.dzbean.res.AccountSecurityEvent;
import sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent;
import sz.szsmk.citizencard.mvp.BasePresenter;
import sz.szsmk.citizencard.mvp.GestureMvpView;
import sz.szsmk.citizencard.mvp.dzsmkpresenter.GesturePresenter;
import sz.szsmk.citizencard.util.Constant;
import sz.szsmk.citizencard.util.JumpUtils;
import sz.szsmk.citizencard.util.StringUtils;
import sz.szsmk.citizencard.widget.GestureContentView;
import sz.szsmk.citizencard.widget.GestureDrawline;

import static sz.szsmk.citizencard.model.network.dzbean.res.MessageEvent.CODE_GESTURESUCCESS;

@Route("gestureLoginActivity")
public class GestureLoginActivity extends AppBaseActivity implements GestureMvpView {

    FrameLayout gestureContainer;
    TextView textTip;
    private GestureContentView mGestureContentView;
    String url;
    @Inject
    GesturePresenter gesturePresenter;
    boolean isNeedVaild;
    private String pwd;
    private int intotype;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getComponent().inject(this);
        setContentView(R.layout.activity_gesture_login);

        gestureContainer = findViewById(R.id.gesture_container);
        textTip = findViewById(R.id.text_tip);
        Intent intent = getIntent();
        url = intent.getStringExtra("url");


        //页面状态栏变成白色
        ImmersionBar.with(this)
                .fitsSystemWindows(true)
                .statusBarDarkFont(true, 0f)
                .statusBarColor(R.color.white).init();
        isNeedVaild = intent.getBooleanExtra("isNeedVaild",false);
        intotype = intent.getIntExtra("intotype",0);

        ImageView iv_back = findViewById(R.id.iv_back);
        iv_back.setOnClickListener(v ->{
                    closeScanQRCodeActivity();

                }
        );


        mGestureContentView = new GestureContentView(this, false, "", new GestureDrawline.GestureCallBack() {
            @Override
            public void onGestureCodeInput(String inputCode) {
//                if (inputCode.length() < 3) {
//                    textTip.setText(Html.fromHtml("<font color='#c70c1e'>至少连接3个点</font>"));
//                    mGestureContentView.clearDrawlineState(0L);
//                    return;
//                }
                pwd= inputCode;
                gestureLogin(inputCode);
                mGestureContentView.clearDrawlineState(0);
            }

            @Override
            public void checkedSuccess() {

            }

            @Override
            public void checkedFail() {

            }
        });
        mGestureContentView.setParentView(gestureContainer);
    }




    @Override
    protected CharSequence getTopTitle() {
        return "验证手势密码";
    }

    private void gestureLogin(String gestureString) {
        if(isNeedVaild){
            gesturePresenter.createGesturePassword(gestureString, null);
        }else{
            gesturePresenter.validGesturePassword(gestureString);
        }

    }

    /**
     * 当手势密码验证错误时
     */
    private void countError() {
        Toast.makeText(this, "手势密码错误", Toast.LENGTH_SHORT).show();
//        Constant.GesErrorCount--;
//        Toast.makeText(this, "手势密码错误，还有" + Constant.GesErrorCount + "次机会", Toast.LENGTH_SHORT).show();
//        if (Constant.GesErrorCount <= 0) {
//            finish();
//        }
    }

    public final static int convertToInt(String value, int defaultValue) {
        if (value == null || "".equals(value.trim())) {
            return defaultValue;
        }

        try {
            return Integer.valueOf(value);
        } catch (Exception e) {
            try {
                return Double.valueOf(value).intValue();
            } catch (Exception e1) {
                return defaultValue;
            }
        }
    }

    @Override
    public void success() {
        Bundle bundle = new Bundle();
        if(url==null){

        } else if(url.equals("gesPasswordSettingActivity")){
            bundle.putBoolean("isNeedVaild",true);
            bundle.putString("oldpwd",pwd);
        }else if(url.equals("identityCodeActivity")){
            bundle.putString(Constant.ID_CARD,getIntent().getStringExtra(Constant.ID_CARD));
            bundle.putString(Constant.ID_NAME,getIntent().getStringExtra(Constant.ID_NAME));
        }else if(url.equals("accountSecurity")){
            Intent intent = new Intent();
            intent.putExtra("code",MessageEvent.CODE_GESTURESUCCESS);
            setResult(100,intent);
        }
        JumpUtils.jumptoNatvie(this, url, 0,bundle);
        finish();
    }

    @Override
    public void fail() {
        countError();
    }

    @Override
    protected BasePresenter[] initPresenters() {
        return new BasePresenter[]{gesturePresenter};
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        closeScanQRCodeActivity();
    }
    private void closeScanQRCodeActivity() {
        if(url==null){
            EventBus.getDefault().post(new InBackgroundBean());
        }
        finish();
    }


}
